﻿using System;

namespace ZOIL.DataModel
{
    /// <summary>
    /// Represents the state of a view on the landscape, including width,height,position and view type
    /// </summary>
    public class VisualProperties : BasePropertyChange, IDatabaseVisualProperties
    {
        private string _viewIndex;
        private string _templateId;
        private double _x;
        private double _y;
        private double _width;
        private double _height;
        private double _opacity;
        private int _zIndex;
        private double _angle;
        private int _viewId;

        #region Implementation of IVisualProperties

        /// <summary>
        /// !Do NOT set the Id yourself!
        /// </summary>
        public long BelongsToDatabaseId { get; set; }
        
        public string ViewIndex
        {
            get { return _viewIndex; }
            set
            {
                if (value == _viewIndex)
                {
                    return;
                }
                _viewIndex = value;
                RaisePropertyChanged("ViewIndex");
                OnObjectLocallyChanged();
            }
        }

        public string TemplateId
        {
            get { return _templateId; }
            set
            {
                if (value == _templateId)
                {
                    return;
                }
                _templateId = value;
                RaisePropertyChanged("TemplateId");
                OnObjectLocallyChanged();
            }
        }

        public double X
        {
            get { return _x; }
            set
            {
                if (value.Equals(_x))
                {
                    return;
                }
                _x = value;
                RaisePropertyChanged("X");
                OnObjectLocallyChanged();
            }
        }

        public double Y
        {
            get { return _y; }
            set
            {
                if (value.Equals(_y))
                {
                    return;
                }
                _y = value;
                RaisePropertyChanged("Y");
                OnObjectLocallyChanged();
            }
        }

        public double Width
        {
            get { return _width; }
            set
            {
                if (value.Equals(_width))
                {
                    return;
                }
                _width = value;
                RaisePropertyChanged("Width");
                OnObjectLocallyChanged();
            }
        }

        public double Height
        {
            get { return _height; }
            set
            {
                if (value.Equals(_height))
                {
                    return;
                }
                _height = value;
                RaisePropertyChanged("Height");
                OnObjectLocallyChanged();
            }
        }

        public double Opacity
        {
            get { return _opacity; }
            set
            {
                if (value.Equals(_opacity))
                {
                    return;
                }
                _opacity = value;
                RaisePropertyChanged("Opacity");
                OnObjectLocallyChanged();
            }
        }

        public int ZIndex
        {
            get { return _zIndex; }
            set
            {
                if (value == _zIndex)
                {
                    return;
                }
                _zIndex = value;
                RaisePropertyChanged("ZIndex");
                OnObjectLocallyChanged();
            }
        }

        public double Angle
        {
            get { return _angle; }
            set
            {
                if (value.Equals(_angle))
                {
                    return;
                }
                _angle = value;
                RaisePropertyChanged("Angle");
                OnObjectLocallyChanged();
            }
        }

        public int ViewId
        {
            get { return _viewId; }
            set
            {
                if (value == _viewId)
                {
                    return;
                }
                _viewId = value;
                RaisePropertyChanged("ViewId");
                OnObjectLocallyChanged();
            }
        }

        #endregion

        #region Implementation of IRemoteUpdateable

        public virtual void OnRemoteUpdated()
        {
            RaisePropertyChanged("ViewIndex");
            RaisePropertyChanged("TemplateId");
            RaisePropertyChanged("X");
            RaisePropertyChanged("Y");
            RaisePropertyChanged("Width");
            RaisePropertyChanged("Height");
            RaisePropertyChanged("Opacity");
            RaisePropertyChanged("ZIndex");
            RaisePropertyChanged("Angle");
            RaisePropertyChanged("ViewId");
        }

        #endregion

        #region Implementation of IObjectLocallyChanged

        public event EventHandler ObjectLocallyChanged;
        protected virtual void OnObjectLocallyChanged()
        {
            EventHandler handler = ObjectLocallyChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        #endregion

        /// <summary>
        /// Creates a default ViewInformation with all Properties set to default values
        /// </summary>
        /// <returns></returns>
        public static VisualProperties Default()
        {
            return new VisualProperties
                {
                    Height = 100,
                    Width = 100,
                    ViewIndex = "default",
                    X = 0.0,
                    Y = 0.0,
                    Opacity = 1.0,
                    ViewId = 0,
                    Angle = 0.0,
                    ZIndex = 0,
                    TemplateId = null
                };
        }

        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        /// <returns>
        /// A string that represents the current object.
        /// </returns>
        public override string ToString()
        {
            return string.Format("VisualProperties [BelongsToDatabaseId: {0}, X: {1}, Y: {2}, Width: {3}, Height: {4}, Opacity: {5}]", BelongsToDatabaseId, X, Y, Width, Height, Opacity);
        }
    }
}